package com.rabbit.library.utils;


import android.os.Environment;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    public final static int SIGN = 1;//签收
    public final static int PROBLEM = 2;//问题件
    public final static int PICKUP = 3;//取件

    public static String getZipName(int type) {
        String zipPath ="";
//        String imgId = MD5Utils.MD5EncodeTo32(  (int) ((Math.random() * 9 + 1) * 10000000) +  System.currentTimeMillis() + Constant.IMGCODE168);
//        String mUserCode= (String) SharePreferenceUitls.get(BaseApplication.getContext(), Constant.INFO_USER_CODE, "");
        String imgId="";
        String mUserCode="";
        switch (type) {
            case SIGN:
                zipPath = Environment.getExternalStorageDirectory().getPath() + "/qianshou" + mUserCode + "-"+imgId + ".zip";
                break;
            case PROBLEM:
                zipPath = Environment.getExternalStorageDirectory().getPath() + "/problem" + mUserCode +"-"+ imgId + ".zip";
                break;
            case PICKUP:
                zipPath = Environment.getExternalStorageDirectory().getPath() + "/yunluqujian" + mUserCode + "-" + imgId + ".zip";
                break;
        }
        return zipPath;
    }

    public static void zip(File[] entries, String dest) throws IOException {
        ZipOutputStream out = null;
        try {
            File outFile = new File(dest);//源文件或者目录
            out = new ZipOutputStream(new FileOutputStream(outFile));
            if(entries!=null){
                for (int i = 0; i < entries.length; i++) {
                    // 递归压缩，更新curPaths
                    zipFileOrDirectory(out, entries[i], "");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            //关闭输出流
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    private static void zipFileOrDirectory(ZipOutputStream out,
                                           File fileOrDirectory, String curPath) throws IOException {
        //从文件中读取字节的输入流
        FileInputStream in = null;
        try {
            //如果此文件是一个目录，否则返回false。
            if (!fileOrDirectory.isDirectory()) {
                // 压缩文件
                byte[] buffer = new byte[4096];
                int bytes_read;
                in = new FileInputStream(fileOrDirectory);
                //实例代表一个条目内的ZIP归档
                ZipEntry entry = new ZipEntry(curPath
                        + fileOrDirectory.getName());
                //条目的信息写入底层流
                out.putNextEntry(entry);
                while ((bytes_read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes_read);
                }
                out.closeEntry();
            } else {
                // 压缩目录
                File[] entries = fileOrDirectory.listFiles();
                for (int i = 0; i < entries.length; i++) {
                    // 递归压缩，更新curPaths
                    zipFileOrDirectory(out, entries[i], curPath
                            + fileOrDirectory.getName() + "/");
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

